
/**   
 * @Title: ICodeApplication.java 
 * @Package: com.rename.icode.activity 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-11 下午5:01:51 
 * @version  
 */


package com.rename.icode.activity;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.util.Log;
import android.widget.Toast;

import com.rename.icode.R;
import com.rename.icode.comm.ApplicationContext;
import com.rename.icode.comm.Constants;
import com.rename.icode.manager.AppConfigManager;
import com.rename.icode.utils.FileUtil;
import com.rename.icode.utils.LogUtil;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/** 
 * @Description 实现了...功能
 * @author WangXinMing
 * @date 2015-2-11 下午5:01:51 
 * @version V1.0
 */

public class ICodeApplication extends Application {

	private final static String TAG = "ICodeApplication";
	private List<Activity> activityList = new LinkedList<Activity>();
	private List<Service> serviceList = new LinkedList<Service>();
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		ApplicationContext.getInstance().init(this);
		
		initAppConfig();
        initRootPath();
	}
	
	/*
	 * 开启应用信息配置管理类
	 */
	private void initAppConfig() {
		AppConfigManager.getInstance();
	}
	

	/**
	 * 
	 * @Description 初始化手机存储卡的根路径，并且创建cache和log文件
	 * @author Administrator
	 */
	private void initRootPath() {
		final String sdcardPath = FileUtil.getSDCardRootDirectory();
		if (sdcardPath == null) {
			Constants.SDCARD_ROOT_PATH = null;
		}
		else {
			String oldPath = AppConfigManager.getInstance().getRootPath();
			LogUtil.i(TAG, "oldPath is " + oldPath);
			LogUtil.i(TAG, "sdcardPath is " + sdcardPath);
			
			if (oldPath == null) {
				LogUtil.i(TAG, "save sdcard path: " + sdcardPath);

				AppConfigManager.getInstance().setRootPath(sdcardPath);
				Constants.SDCARD_ROOT_PATH = sdcardPath;
			}
			else {
				Constants.SDCARD_ROOT_PATH = oldPath;

				if(!oldPath.equals(sdcardPath)) {
					LogUtil.i(TAG, "sdcard path " + sdcardPath + " has changed, the saved path is " + oldPath);

					if(!FileUtil.checkPathValid(oldPath)) {
						Constants.SDCARD_ROOT_PATH = sdcardPath;
					}
				}
				else {
					LogUtil.i(TAG, "sdcard path " + sdcardPath + " has been saved.");

				}
			}
		}
		
		LogUtil.i(TAG, "Constants.SDCARD_ROOT_PATH is " + Constants.SDCARD_ROOT_PATH);
		
		//创建cache、log、code文件夹
		try {
			if(FileUtil.getGlobalCachePath() == null || FileUtil.getGlobalLogPath() == null || FileUtil.getGlobalCodePath() == null){
				Toast.makeText(ApplicationContext.get(), ApplicationContext.get().getString(R.string.sdcard_not_mount), Toast.LENGTH_SHORT
						).show();
				return;
			}
			File cacheFolder = new File(FileUtil.getGlobalCachePath());
			File LogFolder = new File(FileUtil.getGlobalLogPath());
			File codeFolder = new File(FileUtil.getGlobalCodePath());
			FileUtil.createDir(cacheFolder);
			FileUtil.createDir(LogFolder);
			FileUtil.createDir(codeFolder);
			
		} catch (NullPointerException e) {
			LogUtil.e(TAG, "init root path encouter null pointer exception");
			Toast.makeText(ApplicationContext.get(), ApplicationContext.get().getString(R.string.sdcard_not_mount), Toast.LENGTH_SHORT
					).show();
			LogUtil.e(TAG, "init root path encouter null pointer exception,after toast show");
		} catch (Exception e) {
			LogUtil.e(TAG, "init root path encouter exception");
			Toast.makeText(getApplicationContext(), "sdcard mount exception,couldn't launch mixin APP", Toast.LENGTH_SHORT
					).show();
			e.printStackTrace();
		}

	}
	
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}
	
	public void addService(Service service) {
		serviceList.add(service);
	}
	
	public void exit(){
		for (Activity activity : activityList) {
			Log.i(TAG, "activity finish");
			activity.finish();
		}
		
		for (Service service : serviceList) {
			service.stopSelf();
		}
		
		System.exit(0);
	}
	
}
